/**
 * @param {number} n
 * @param {number} k
 * @return {number[][]}
 */
var combine = function (n, k) {
  let res = []
  let path = []
  backtrace(1)
  function backtrace(index){
    if(path.length==k){
      res.push([...path])
      return
    }

    for(let i=index;i<=n;i++){
      path.push(i)
      backtrace(i+1)
      path.pop()
    }
  }
  return res
}

combine(4, 2)
